package com.lzh.async.config;

import com.lzh.async.properties.LzhAsyncTaskProperties;
import com.lzh.async.thread.LzhThreadPoolTaskExecutor;
import com.lzh.async.thread.RequestContextAwarePoolExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author zhehen.lu
 * @date 2025/8/13 19:48
 */
@EnableAsync(
        proxyTargetClass = true
)
public class LzhAsyncTaskConfig {
    @Autowired
    private LzhAsyncTaskProperties taskProperties;

    @Bean
    public ThreadPoolTaskExecutor yyRequestWrapperThreadPoolTaskExecutor() {
        RequestContextAwarePoolExecutor executor = new RequestContextAwarePoolExecutor();
        executor.setCorePoolSize(this.taskProperties.getCorePoolSize());
        executor.setMaxPoolSize(this.taskProperties.getMaxPoolSize());
        executor.setQueueCapacity(this.taskProperties.getQueueSize());
        executor.setThreadNamePrefix(this.taskProperties.getThreadNamePrefix() + "-");
        executor.setKeepAliveSeconds(this.taskProperties.getKeepAliveTime());
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        executor.initialize();
        return executor;
    }

    @Bean
    public ThreadPoolTaskExecutor yyThreadPoolTaskExecutor() {
        LzhThreadPoolTaskExecutor executor = new LzhThreadPoolTaskExecutor();
        executor.setCorePoolSize(this.taskProperties.getCorePoolSize());
        executor.setMaxPoolSize(this.taskProperties.getMaxPoolSize());
        executor.setQueueCapacity(this.taskProperties.getQueueSize());
        executor.setThreadNamePrefix(this.taskProperties.getThreadNamePrefix() + "-");
        executor.setKeepAliveSeconds(this.taskProperties.getKeepAliveTime());
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        executor.initialize();
        return executor;
    }

    public LzhAsyncTaskConfig() {
    }

    public LzhAsyncTaskProperties getTaskProperties() {
        return this.taskProperties;
    }

    public void setTaskProperties(LzhAsyncTaskProperties taskProperties) {
        this.taskProperties = taskProperties;
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof LzhAsyncTaskConfig)) {
            return false;
        } else {
            LzhAsyncTaskConfig other = (LzhAsyncTaskConfig)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                Object this$taskProperties = this.getTaskProperties();
                Object other$taskProperties = other.getTaskProperties();
                if (this$taskProperties == null) {
                    if (other$taskProperties != null) {
                        return false;
                    }
                } else if (!this$taskProperties.equals(other$taskProperties)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof LzhAsyncTaskConfig;
    }

    @Override
    public int hashCode() {
        int result = 1;
        Object $taskProperties = this.getTaskProperties();
        result = result * 59 + ($taskProperties == null ? 43 : $taskProperties.hashCode());
        return result;
    }

    @Override
    public String toString() {
        return "LzhAsyncTaskConfig(taskProperties=" + this.getTaskProperties() + ")";
    }
}
